package com.project.service.IMP;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.Reader;

/*
* 所有业务组件的弗莱
* */
public class BaseService {
    private static SqlSessionFactory factory;
//    让读取操作只读一次，减少流操作
    static {
    try {
        Reader rs = Resources.getResourceAsReader("mybatis.cfg.xml");
        factory = new SqlSessionFactoryBuilder().build(rs);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

    public SqlSession getSession(){
        return factory.openSession();
    }
}
